package com.ruoyi.dispatch.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.dispatch.domain.MonthYield;
import com.ruoyi.dispatch.enums.WorkfaceDayReportCol;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.dispatch.mapper.WorkfaceDayReportMapper;
import com.ruoyi.dispatch.domain.WorkfaceDayReport;
import com.ruoyi.dispatch.service.IWorkfaceDayReportService;

/**
 * 产量日报Service业务层处理
 *
 * @author wuxiaofang
 * @date 2025-04-08
 */
@Service
public class WorkfaceDayReportServiceImpl implements IWorkfaceDayReportService {
    @Autowired
    private WorkfaceDayReportMapper workfaceDayReportMapper;

    /**
     * 查询产量日报
     *
     * @param reportId 产量日报主键
     * @return 产量日报
     */
    @Override
    public WorkfaceDayReport selectWorkfaceDayReportByReportId(Long reportId) {
        return workfaceDayReportMapper.selectWorkfaceDayReportByReportId(reportId);
    }

    /**
     * 查询产量日报列表
     *
     * @param workfaceDayReport 产量日报
     * @return 产量日报
     */
    @Override
    public List<WorkfaceDayReport> selectWorkfaceDayReportList(WorkfaceDayReport workfaceDayReport) {
        return workfaceDayReportMapper.selectWorkfaceDayReportList(workfaceDayReport);
    }

    /**
     * 新增产量日报
     *
     * @param workfaceDayReport 产量日报
     * @return 结果
     */
    @Override
    public int insertWorkfaceDayReport(WorkfaceDayReport workfaceDayReport) {
        workfaceDayReport.setCreateTime(DateUtils.getNowDate());
        return workfaceDayReportMapper.insertWorkfaceDayReport(workfaceDayReport);
    }

    /**
     * 修改产量日报
     *
     * @param workfaceDayReport 产量日报
     * @return 结果
     */
    @Override
    public int updateWorkfaceDayReport(WorkfaceDayReport workfaceDayReport) {
        workfaceDayReport.setUpdateTime(DateUtils.getNowDate());
        return workfaceDayReportMapper.updateWorkfaceDayReport(workfaceDayReport);
    }

    /**
     * 批量删除产量日报
     *
     * @param reportIds 需要删除的产量日报主键
     * @return 结果
     */
    @Override
    public int deleteWorkfaceDayReportByReportIds(Long[] reportIds) {
        return workfaceDayReportMapper.deleteWorkfaceDayReportByReportIds(reportIds);
    }

    /**
     * 删除产量日报信息
     *
     * @param reportId 产量日报主键
     * @return 结果
     */
    @Override
    public int deleteWorkfaceDayReportByReportId(Long reportId) {
        return workfaceDayReportMapper.deleteWorkfaceDayReportByReportId(reportId);
    }

    @Override
    public List<Map<String, String>> handleColumnMap(String[] columnArray) {
        List<Map<String, String>> columns = Lists.newArrayList();
        for (String item : columnArray) {
            Map column = WorkfaceDayReportCol.getByCode(item);
            if (column != null) {
                columns.add(column);
            }
        }
        return columns;
    }

    @Override
    public List<MonthYield> countMonthYield(WorkfaceDayReport workfaceDayReport) {
        List<WorkfaceDayReport> dayReports = workfaceDayReportMapper.selectWorkfaceDayReportList(workfaceDayReport);
        SimpleDateFormat sourceFormatMonth = new SimpleDateFormat("yyyy-MM");
        SimpleDateFormat sourceFormatYear = new SimpleDateFormat("yyyy");
        String month = sourceFormatMonth.format(workfaceDayReport.getCurrentDate());
        String year = sourceFormatYear.format(workfaceDayReport.getCurrentDate());
        List<MonthYield> monthYields = workfaceDayReportMapper.countMonthYield(month);
        List<MonthYield> yearYields = workfaceDayReportMapper.countYearYield(year);
        for (WorkfaceDayReport dayReport : dayReports) {
            for (MonthYield monthYield : monthYields) {
                if (null != monthYield && null != dayReport) {
                    if (dayReport.getMiningTeamName().equals(monthYield.getMiningTeamName()) && dayReport.getWorkfaceName().equals(monthYield.getWorkfaceName())) {
                        BeanUtils.copyProperties( dayReport,monthYield);
                        monthYield.setDifference(dayReport.getYield().subtract(dayReport.getDayPlan()));
                    }
                }
                for (MonthYield yearYield : yearYields) {
                    if (null != yearYield && null != monthYield){
                        if (monthYield.getMiningTeamName().equals(yearYield.getMiningTeamName()) && monthYield.getWorkfaceName().equals(yearYield.getWorkfaceName())) {
                            monthYield.setYearPlan(yearYield.getYearPlan());
                            monthYield.setYearYield(yearYield.getYearYield());
                        }
                    }
                }
            }

        }
        return monthYields;
    }
}
